package com.malinkang.sqlite;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;

import static com.malinkang.sqlite.DBManager.getOrderDao;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();


    @Bind(R.id.listview) ListView mListView;
    @Bind(R.id.btn_add) Button btnAdd;
    private UserAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Cursor cursor = getContentResolver().query(Uri.parse("content://" + UserContentProvider.sAuthority + "/" + DBOpenHelper.USER_TABLE_NAME), null, null, null, null);
        mAdapter = new UserAdapter(this, cursor);
        mListView.setAdapter(mAdapter);
        DBManager.getStudentDao(this).deleteAll();
        Student student = new Student(0, 20, "test0");
        DBManager.getStudentDao(this).insert(student);//插入单条
        List<Student> students = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Student s = new Student(i + 1, i + 21, "test" + (i + 1));
            students.add(s);
        }
        DBManager.getStudentDao(this).insertInTx(students);//批量插入

        students = DBManager.getStudentDao(this).queryRaw("where age>?", "25");
        for (Student student1 : students) {
            Log.d(TAG, student1.toString());
        }
        //delete where
        //deleteAll删除所有
        //删除年龄大于25的
        DBManager.getStudentDao(this).queryBuilder().where(StudentDao.Properties.Age.gt(25))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities();

        students = DBManager.getStudentDao(this).queryBuilder().list();
        Log.d(TAG, "------------------");

        for (Student student1 : students) {
            Log.d(TAG, student1.toString());
        }

        //更新 不支持直接update 先查询再更新
        Student s = DBManager.getStudentDao(this).queryBuilder().where(StudentDao.Properties.Age.eq(20)).unique();
        s.setAge(40);
        DBManager.getStudentDao(this).update(s);
        Log.d(TAG, "------------------");

        for (Student student1 : students) {
            Log.d(TAG, student1.toString());
        }

        Order order = new Order();
        Customer customer = new Customer();
        customer.setId(123L);
        customer.setName("test");
        order.setCustomer(customer);
        getOrderDao(this).insert(order);
        Log.d(TAG,"order="+ order);

       // getCustomerDao(this).insert(customer);
        List<Order> orders=DBManager.getOrderDao(this).queryBuilder().list();
        for (Order order1 : orders) {
            Log.d(TAG,"customer="+ order1.getCustomer());
            Log.d(TAG,"order1="+ order1);

        }

        DBManager.getStudentDao(this).rx().loadAll()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<Student>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<Student> students) {

                    }
                });
    }

    @OnClick(R.id.btn_add)
    public void addUser() {
        Intent intent = new Intent(this, InsertOrUpdateActivity.class);
        startActivity(intent);
    }


}
